<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\TenantProduct\Service;

use Hyperf\Database\Model\Builder;
use MoChat\Framework\Service\AbstractService;
use MoChat\Plugin\TenantProduct\Contract\TenantProductSkuContract;
use MoChat\Plugin\TenantProduct\Model\TenantProductAttribute;
use MoChat\Plugin\TenantProduct\Model\TenantProductSku;
use MoChat\Plugin\TenantProduct\Model\TenantProductValue;

class TenantProductSkuService extends AbstractService implements TenantProductSkuContract
{
    /**
     * @var TenantProductSku
     */
    protected $model;

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductSkuById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 查询多条 - 根据ID.
     * @param array $ids ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductSkusById(array $ids, array $columns = ['*']): array
    {
        return $this->model->getAllById($ids, $columns);
    }

    /**
     * 多条分页.
     * @param array $where 查询条件
     * @param array|string[] $columns 查询字段
     * @param array $options 可选项 ['orderByRaw'=> 'id asc', 'perPage' => 15, 'page' => null, 'pageName' => 'page']
     * @return array 分页结果 Hyperf\Paginator\Paginator::toArray
     */
    public function getTenantProductSkuList(array $where, array $columns = ['*'], array $options = []): array
    {
        return $this->model->getPageList($where, $columns, $options);
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createTenantProductSku(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 添加多条
     * @param array $data 添加的数据
     * @return bool 执行结果
     */
    public function createTenantProductSkus(array $data): bool
    {
        return $this->model->createAll($data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateTenantProductSkuById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 删除 - 单条
     * @param int $id 删除ID
     * @return int 删除条数
     */
    public function deleteTenantProductSku(int $id): int
    {
        return $this->model->deleteOne($id);
    }

    /**
     * 删除 - 多条
     * @param array $ids 删除ID
     * @return int 删除条数
     */
    public function deleteTenantProductSkus(array $ids): int
    {
        return $this->model->deleteAll($ids);
    }

    /**
     * 查询所有.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductSkus(array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->get($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 商品属性.
     */
    public function createTenantProductSkuAttribute(array $data): int
    {
        return (new TenantProductAttribute())->createOne($data);
    }

    /**
     * 商品属性.
     */
    public function createTenantProductSkuValue(array $data): int
    {
        return (new TenantProductValue())->createOne($data);
    }

    /**
     * @param array|string[] $columns
     */
    public function getTenantProductSkuBySpuId(int $spuId, string $startTime, string $endTime, int $status, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('spu_id', $spuId)
            ->when(! empty($startTime), function (Builder $query) use ($startTime, $endTime) {
                return $query->whereBetween('created_at', [$startTime, $endTime]);
            })
            ->when(! empty($status), function (Builder $query) use ($status) {
                return $query->where('status', $status);
            })
            ->orderByDesc('created_at')
            ->get($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    public function getTenantProductSkuValuesById(array $ids): array
    {
        $res = TenantProductValue::from(TenantProductValue::query()->getModel()->getTable() . ' as tenant_product_value')
            ->join(TenantProductAttribute::query()->getModel()->getTable() . ' as tenant_product_attribute', 'tenant_product_value.attribute_id', 'tenant_product_attribute.id')
            ->whereIn('tenant_product_value.id', $ids)
            ->get(['tenant_product_attribute.id as attributeId', 'tenant_product_attribute.name as attributeName', 'tenant_product_value.id as valueId', 'tenant_product_value.value as attributeValue']);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateTenantProductSkuAttributeById(int $id, array $data): int
    {
        return (new TenantProductAttribute())->updateOneById($id, $data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateTenantProductSkuValueById(int $id, array $data): int
    {
        return (new TenantProductValue())->updateOneById($id, $data);
    }

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductSkuByIdWithTrashed(int $id, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('id', $id)
            ->withTrashed()
            ->first($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 查询单条 - 根据名称.
     * @param string $name ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductAttributesByName(string $name, array $columns = ['*']): array
    {
        $res = (new TenantProductAttribute())->where(['name' => $name, 'status' => 1])->get($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 查询单条 - 根据名称属性id.
     * @param int $attributeId 属性ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductSkuByAttributeId(int $attributeId, array $columns = ['*']): array
    {
        $res = $this->model->whereRaw("find_in_set({$attributeId},attributes)")->first($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 查询单条 - 根据名称属性id.
     * @param int $attributeId 属性ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getTenantProductValueByAttributeId(int $attributeId, array $columns = ['*']): array
    {
        $res = (new TenantProductValue())->where('attribute_id', $attributeId)->first($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }
}
